package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.nursing.NursingLevel;
import com.zzyl.entity.nursing.NursingPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingLevelServiceImpl implements NursingLevelService {
	
	private final NursingLevelMapper nursingLevelMapper;
	private final NursingPlanMapper nursingPlanMapper;
	
	@Override
	public List<NursingLevelVo> queryNursingLevelList() {
		return nursingLevelMapper.selectNursingLevelVoList();
	}
	
	@Override
	public void createNursingLevel(NursingLevelDto nursingLevelDto) {
		NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
		nursingLevelMapper.insertOne(nursingLevel);
	}
	
	@Override
	public PageBean<NursingLevelVo> queryNursingLevelPage(Integer pageNum, Integer pageSize, String name, Integer status) {
		pageNum = Math.max(pageNum, 1);
		pageSize = Math.max(pageSize, 5);
		PageHelper.startPage(pageNum, pageSize);
		Page<NursingLevelVo> page = nursingLevelMapper.selectPage(NursingLevel.builder().name(name).status(status).build());
		return PageBean.of(page);
	}
	
	@Override
	public NursingLevelVo queryNursingLevelDetail(Long id) {
		return nursingLevelMapper.selectVoById(id);
	}
	
	@Override
	public void updateNursingLevel(NursingLevelDto nursingLevelDto) {
		Long id = nursingLevelDto.getId();
		Assert.notNull(id);
		NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
		nursingLevelMapper.updateById(nursingLevel);
	}
	
	@Override
	public void deleteNursingLevel(Long id) {
		nursingLevelMapper.deleteById(id);
	}
	
	@Override
	public void updateNursingLevelStatus(Long id, Integer status) {
		NursingLevel nursingLevel = new NursingLevel();
		nursingLevel.setId(id);
		nursingLevel.setStatus(status);
		nursingLevelMapper.updateById(nursingLevel);
	}

	@Override
	public List<NursingLevelVo> listAll() {
		List<NursingLevelVo> list = nursingLevelMapper.listAll();
		return list;
	}

	@Override
	public void InsertAll(NursingLevelDto nursingLevelDto) {
		NursingPlan nursingPlan = BeanUtil.copyProperties(nursingLevelDto, NursingPlan.class);
		nursingPlanMapper.InstNursingPlan(nursingPlan);
		nursingLevelMapper.InsertAll(nursingLevelDto);
	}

	@Override
	public PageBean<NursingLevelVo> PageAll(Integer pageNum, Integer pageSize, String name, Integer status) {
		pageNum  = Math.max(pageNum, 1);
		pageSize = Math.max(pageSize, 1);
		PageHelper.startPage(pageNum, pageSize);
		Page<NursingLevelVo> page = nursingLevelMapper.PageAll(name, status);
		return PageBean.of(page);
	}

	@Override
	public NursingLevelVo GetidAll(Integer id) {
		return nursingLevelMapper.GetidAll(id);
	}

	@Override
	public void uqdateidAll(NursingLevelVo nursingLevelVo) {
		NursingPlan nursingPlan = BeanUtil.copyProperties(nursingLevelVo,NursingPlan.class);
		nursingPlanMapper.updataNationList(nursingPlan);
		nursingLevelMapper.uqdateidAll(nursingLevelVo);
	}

	@Override
	public void DeleteAll(Integer id) {
	    Integer planId = nursingLevelMapper.resture(id);
		nursingLevelMapper.DeleteAll(id);
		nursingPlanMapper.deleteById(Long.valueOf(planId));
	}

	@Override
	public void BootdisableAll(Integer id, Integer status) {
		nursingLevelMapper.BootdisableAll(id,status);
	}
}
